package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.base.BaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.expimp.util.AbstractExpUtil;
import com.ccp.dev.qbdms.expimp.util.AuxiliaryUtil;
import com.ccp.dev.qbdms.model.WQbdFlightTest;
import com.ccp.dev.qbdms.model.vo.ZeroFaultInformationStatisticsExportVo;
import com.ccp.dev.qbdms.service.ZeroFaultInformationStatisticsService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.BaseUtil.SystemLevel;
import com.google.common.collect.Maps;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ClassDescribe:
 *
 * @author :milihao
 * @date :2020-07-21 Since:1
 */
@Controller
@Action(customModel = "靶场零故障信息统计")
@RequestMapping("/qbdms/zeroFaultInformationStatistics")
public class ZeroFaultInformationStatisticsController extends BaseController {

	@Resource
	private ZeroFaultInformationStatisticsService zeroFaultInformationStatisticsService;
	@Resource
	public ScriptImpl scriptimpl;

	/**
	 * 功能描述: 跳转到型号质量列表页
	 *
	 * @return: java.lang.String
	 * @auther: mlh
	 * @date: 2020-07-21 09:12:57
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
	@Action(description = "跳转到靶场零故障信息统计页面", exectype = "页面跳转日志")
	public String list(ModelMap modelMap) {
		modelMap.put("isGroup", false);
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		if (currentSystemLevel.equals(SystemLevel.GROUPLEVEL)) {
			modelMap.put("isGroup", true);
		}
		return "zeroFaultInformationStatistics/list";
	}

	/**
	 * 功能描述: 获取所有统计图数据
	 *
	 * @param: WQbdReportTemplate 实体
	 * @return: java.lang.Object
	 * @auther: milihao
	 * @date: 2020-7-21 15:26:43
	 */
	@RequestMapping("/getChartData")
	@ResponseBody
	@Action(description = "获取统计图数据", detail = "获取所有统计图数据<#if success>成功<#else>失败</#if>")
	public Object getCharData(HttpServletRequest request) {
		QueryFilter queryFilter = new QueryFilter(request, true);
		List<Map<String, Object>> dataMapList = zeroFaultInformationStatisticsService
				.getChartData(queryFilter);
		SysAuditThreadLocalHolder.putParamerter("success", true);
		return success(dataMapList, dataMapList.size());
	}

	/**
	 * 功能描述: 跳转到导出选择字段页面
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/export")
	@Action(description = "跳转到转到靶场零故障数据导出选择字段页面", exectype = "页面跳转日志")
	public String export(HttpServletRequest request, ModelMap modelMap) throws Exception {
		com.alibaba.fastjson.JSONObject jsonObject = AuxiliaryUtil
				.resolverModel(ZeroFaultInformationStatisticsExportVo.class);
		modelMap.put("tableInfo", jsonObject.toString());
		return "expImp/add";
	}

	/**
	 * 功能描述: 导出发动机试车数据信息
	 *
	 * @param: [request] http请求
	 * @return: java.lang.Object
	 * @auther: sun
	 * @date: 2020-07-22 08:45:02
	 */
	@RequestMapping("/exportData")
	@Action(description = "导出靶场零故障数据信息", detail = "导出靶场零故障数据信息<#if success>成功<#else></#if>")
	public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
		QueryFilter queryFilter = new QueryFilter(request, false);
		List<Map<String, Object>> dataMapList = zeroFaultInformationStatisticsService
				.getChartData(queryFilter);
		List<ZeroFaultInformationStatisticsExportVo> zeroFaultInformationStatisticsExportVoList = Lists
				.newArrayList();
		ZeroFaultInformationStatisticsExportVo zeroFaultInformationStatisticsExportVo;
		for (Map map : dataMapList) {
			if(null==map.get("FILLDEPTHOSPITAL")){
				map.put("FILLDEPTHOSPITAL","");
			}
			zeroFaultInformationStatisticsExportVo = new ZeroFaultInformationStatisticsExportVo(
					map.get("COUNTA").toString(), map.get("COUNTB").toString(), map.get("COUNTC").toString(),
					map.get("COUNTD").toString(),
					map.get("COUNTE").toString(), map.get("COUNTF").toString(),
					map.get("FILLDEPTHOSPITAL").toString()
					);
			zeroFaultInformationStatisticsExportVoList.add(zeroFaultInformationStatisticsExportVo);
		}
		if (zeroFaultInformationStatisticsExportVoList.size() == 0) {
			zeroFaultInformationStatisticsExportVoList.add(new ZeroFaultInformationStatisticsExportVo());
		}
		Map<String, Object> filedMap = Maps.newHashMap();
		filedMap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
		JSONArray jsonArray = ZhiLiangConst.exportAddConditions(queryFilter, new String[0]);
		new AbstractExpUtil() {
			@Override
			public String customAnalysis(String fieldName, String fieldValue) {
				return null;
			}
		}.executeExport(request, response, zeroFaultInformationStatisticsExportVoList, jsonArray,
				AbstractExpUtil.ExpStyle.MANY_SHEET);
		SysAuditThreadLocalHolder.putParamerter("success", true);
	}

}
